﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
using 远程监控屏幕服务端.Models;

namespace 远程监控屏幕服务端.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "远程监控屏幕服务端";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        private int col;

        public int Col
        {
            get { return col; }
            set { col = value; RaisePropertyChanged(); }
        }
        private int row;

        public int Row
        {
            get { return row; }
            set { row = value; RaisePropertyChanged(); }
        }

        public DelegateCommand Two2TwoCommand { get; set; }
        public DelegateCommand ToOriginCommand { get; set; }

        public ObservableCollection<ImageModel> DataList { get; set; }
        List<TcpClient> clients;
        public MainWindowViewModel()
        {

            DataList = new ObservableCollection<ImageModel>();
            Start();

            Two2TwoCommand = new DelegateCommand(() =>
            {
                Col = 2;
                Row= 0;
            });
            ToOriginCommand = new DelegateCommand(() =>
            {
                Col = 0;
                Row = 0;
            });

            Task.Run(async () =>
            {
                while (true)
                {
                    int count = DataList.Count;
                    for (int i = 0; i < count; i++)
                    {
                        NetworkStream ns = clients[i].GetStream();

                        List<byte> bList = new List<byte>();
                        byte[] buffer = new byte[1024 * 1024 * 3];
                        int bytesRead;

                        bytesRead = await ns.ReadAsync(buffer, 0, buffer.Length)-4;

                        int size = BitConverter.ToInt32(buffer.Take(4).ToArray(), 0);
                        bList.AddRange(buffer.Skip(4).Take(bytesRead).ToArray());

                        if (bList.Count!=size)
                        {
                            continue;
                        }
                        //while (bList.Count != size)
                        //{
                        //    bytesRead = await ns.ReadAsync(buffer, 0, buffer.Length);
                        //    bList.AddRange(buffer.Take(bytesRead));
                        //}

                        // ns.Write(new byte[1], 0, 1);

                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            try
                            {
                                BitmapImage b = new BitmapImage();
                                b.BeginInit();
                                b.StreamSource = new MemoryStream(bList.ToArray());
                                b.EndInit();

                                DataList[i].Image = b;
                            }
                            catch (Exception)
                            {

                            }
                           
                        });


                    }
                }


            });


        }


        public void Start()
        {
            Task.Run(() =>
            {
                clients = new List<TcpClient>();

                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 12561);
                TcpListener tcp = new TcpListener(ipep);
                tcp.Start(100);


                while (true)
                {
                    TcpClient acc = tcp.AcceptTcpClient();
                    clients.Add(acc);
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        DataList.Add(new ImageModel() { Image = new System.Windows.Media.Imaging.BitmapImage() });
                    });
                }
            });

        }
    }


}
